﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;

public partial class administrator_usercontrol_usermanager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["cm"] == null)
            {
                List<KhachHangBUS> khachhangs = new List<KhachHangBUS>();
                khachhangs = KhachHangBUS.GetAll();
                GridView1.AutoGenerateColumns = false;
                GridView1.DataSource = khachhangs;
                GridView1.DataBind();
            }
            else
            {
                string cm = Request.QueryString["cm"];
                if (cm == "edit" && Request.QueryString["ID"] != null)
                {
                    mt1.ActiveViewIndex = 1;
                    int ID = Convert.ToInt32(Request.QueryString["ID"]);
                    KhachHangBUS kh = KhachHangBUS.GetByID(ID);
                    if (kh != null)
                    {
                        LbID.Text = kh.ID.ToString();
                        TextBox1.Text = kh.TenDangNhap;
                        TextBox2.Text = kh.HoTen;
                        TextBox8.Text = kh.CMND;
                        TextBox3.Text = kh.SoDienThoai;
                        TextBox4.Text = kh.Email;
                        TextBox5.Text = kh.CauHoi;
                        TextBox9.Text = kh.TraLoi;
                        TextBox6.Text = kh.DiaChi;
                        TextBox10.Text = kh.GhiChu;
                    }
                }
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("khachhang.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int ID = Convert.ToInt32(LbID.Text);
        KhachHangBUS kh = KhachHangBUS.GetByID(ID);
        kh.GhiChu = TextBox10.Text;
        kh.UpDateGhiChu();
        Response.Redirect("khachhang.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        List<KhachHangBUS> khachhangs = new List<KhachHangBUS>();
        khachhangs = KhachHangBUS.GetAll();
        GridView1.DataSource = khachhangs;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            Response.Redirect("khachhang.aspx?cm=edit&ID=" + e.CommandArgument.ToString());
        }
        if (e.CommandName == "Delete")
        {
            int ID = Convert.ToInt32(e.CommandArgument);
            KhachHangBUS kh = KhachHangBUS.GetByID(ID);
            try
            {
                kh.Delete();
                Response.Redirect("khachhang.aspx");
            }
            catch
            {
                Lbthongbao.Text = "Không Thể Xóa Dữ Liệu !!!";
            }

        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lkbXoa");

            l.Attributes.Add("onclick", "javascript:return " +
                 "confirm('bạn có muốn xóa khách hàng " +
                 DataBinder.Eval(e.Row.DataItem, "TenDangNhap") + " ?')");

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}
